angular.module('Controller')

.controller 'WFLoginCtrl', ($scope, $state,cryptUtil,commonUtil,userData,$localStorage,commonConstant,wfAlert,$http) ->
    userData.setStore(9999)
    _.extend $scope,
        data:
            account:''
            username:''
            password:''
        login:->
#            if $scope.data.username=='' or $scope.data.username==undefined
#                $scope.data.username='ogcloud'
#                $scope.data.password='ogcloud'
            postData={
                account:'ogcloud'
                username:$scope.data.username
                password:cryptUtil.hex_md5($scope.data.password)
            }
            getParam=commonUtil.transferGetParam(postData)
            $http.get('@ostengar/login'+getParam)
            .success((resp)->
                if resp.code==0
                    $localStorage.$reset()
                    user=angular.copy resp.message
                    userData.setUser user
                    userData.setToken resp.message.token
                    $http.get('@ostengar/adminUser/'+user.userId)
                        .success((resp)->
                            console.log('adminUser',resp)
                            if resp.code==0
                                if(resp.message.store!=null && resp.message.store!=undefined)
                                    userData.setStore(resp.message.store)
                                else
                                    userData.setStore(undefined)
                                userData.setAccount resp.message.accountId
                                $state.go 'main'
                            else
                                wfAlert.toastError('账号信息获取异常，登录失败')
                    )
                else
                    wfAlert.toastError(resp.message)
            )

